package com.fengye.security.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AuthenticationEvent {

    @EventListener
    public void onSuccess(AuthenticationSuccessEvent event){
        Authentication authentication = event.getAuthentication();
        log.info("authentication = {}", authentication);

        Object source = event.getSource();
        log.info("source = {}", source);
    }

    @EventListener
    public void onFailure(AbstractAuthenticationFailureEvent event){
        Authentication authentication = event.getAuthentication();
        log.info("authentication = {}", authentication);

        Object source = event.getSource();
        log.info("source = {}", source);

        AuthenticationException exception = event.getException();
        log.info("登录失败信息： {}", exception.getMessage());
    }
}
